<?php
if (!ob_start("ob_gzhandler")) {
  ob_start();
}

require_once '../include/config.php';
require_once '../include/utils.class.php';
require_once '../include/orm.class.php';

$__sys = Utils::get_param('__sys', NULL);
$__inst = Utils::get_param('__inst', NULL);
$__md = Utils::get_param('__md', NULL);
$__file = Utils::get_param('__file', 'index');
$__iframe = Utils::get_param('__iframe', 0);

$session_id = Utils::get_param('PHPSESSID', NULL);

if ($session_id) {
  session_id($session_id);
  session_start();
} else {
  session_name(SESSION);
  session_start();
}

if (!isset($_SESSION[SYS . $__sys])) {
  die('Session Not Found');
}

$app_data = $_SESSION[SYS . $__sys];
$inst = $app_data->instances[$__inst];

if (!in_array($inst->__folder, $app_data->plugins)) {
  die('Acceess Denied');
}

if ($app_data->sys->login_required == 1 && (!(isset($app_data->is_authorised)) || $app_data->is_authorised == 0) && !in_array($inst->__folder, $app_data->exceptions)) {
  die('Not Authenticated');
}

require_once PLGN_DIR . "$inst->__folder/$__file.srv.php";
$orm = new ORM();
Server::$__md($orm, $__sys, $inst, $__iframe);
$orm->close();
?>